package org.example.user.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.example.user.domain.Dept;
import org.example.user.domain.SysDictionaryContent;
import org.example.user.service.DeptService;
import org.example.user.service.SysDictionaryContentService;
import org.example.user.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @RequestMapping("/list")
    public Result list(){
        List<Dept> list = deptService.list();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setDeep(3);
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<Dept, String>build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getPersonId()+"");
                    tree.setName(treeNode.getName());
                    tree.putExtra("sort",treeNode.getSort());
                    tree.putExtra("status",treeNode.getStatus());
                    tree.putExtra("createDate",treeNode.getCreateDate());
                });
        System.out.println("111");
        return Result.success(treeNodes);
    }
    @RequestMapping("/list2")
    public Result list2(@RequestBody Dept dept){
        List<Dept> list = deptService.listAll(dept);
        return Result.success(list);
    }
    @RequestMapping("list3")
    public Result list3(){
        List<Dept> list = deptService.listDepts();
        return Result.success(list);
    }
    @RequestMapping("deptList")
    public Result deptList(){
        List<Dept> list = deptService.list();
        return Result.success(list);
    }
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    SysDictionaryContentService service;
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @RequestMapping("/addDept")
    public Result addDept(@RequestBody Dept dept){
        //添加之前先删除缓存
        redisTemplate.delete("contentList"+4);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        dept.setCreateDate(format);
        boolean b = deptService.save(dept);
        SysDictionaryContent sysDictionaryContent = new SysDictionaryContent();
        sysDictionaryContent.setCreatedDate(new Date());
        sysDictionaryContent.setTitle(dept.getName());
        sysDictionaryContent.setSort(dept.getSort()+"");
        sysDictionaryContent.setStatus("正常");
        sysDictionaryContent.setRemark("部门"+dept.getName());
        sysDictionaryContent.setDictId(4);
        sysDictionaryContent.setVal(dept.getId()+"");
        service.save(sysDictionaryContent);
        //发送消息删除缓存
        if(b){
            Message<Dept> message = MessageBuilder.withPayload(dept).build();
            rocketMQTemplate.syncSend("test-topic",message,1000,1);
        }
        return Result.success();
    }
    @RequestMapping("/del")
    public Result del(String id){
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dept::getId,id);
        Dept dept = deptService.getOne(wrapper);
        dept.setStatus(0);
        deptService.updateById(dept);
        return Result.success();
    }
    @RequestMapping("/openDept")
    public Result openDept(Integer id){
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dept::getId,id);
        Dept dept = deptService.getOne(wrapper);
        dept.setStatus(1);
        deptService.updateById(dept);
        return Result.success();
    }


}
